﻿Chenxin.UI.EmpSettingMgr = Chenxin.extend(Chenxin.UI.PageCommon, {
    actionUrl: '/service/logic/EmpSettingMgr.ashx',
    idList: [],
    createGrid: function() {

        return new Chenxin.UI.JsonGrid({
            actionUrl: this.actionUrl + '?op=getlist',
            id: 'esmtable',
            pageSize: 10,
            cellBorder: true,
            modelId: 'RecordId',
            title: '交易设定管理',
            renderTo: 'esm_table',
            columns: {
                UserCode: {
                    head: '工号',
                    width: 100,
                    sortable: true
                },
                UserName: {
                    head: '姓名',
                    width: 120,
                    sortable: true
                },
                Company: {
                    head: '公司',
                    width: 80,
                    sortable: true
                },
                DepartName: {
                    head: '部门',
                    width: 80,
                    sortable: true
                },
                IsControl: {
                    width: 50,
                    head: '管辖',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchContractArchive: {
                    width: 80,
                    head: '查询报备',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchContractApply: {
                    width: 90,
                    head: '查询业绩申请',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchIntention: {
                    width: 90,
                    head: '查询意向书',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchBargain: {
                    width: 90,
                    head: '查询合同/成交',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchMoney: {
                    width: 90,
                    head: '查询意向金',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchBill: {
                    width: 90,
                    head: '查询开单',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchPaidItems: {
                    width: 110,
                    head: '查询我的实收',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchCommisionDetail: {
                    width: 110,
                    head: '查询未收佣收',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchIncomeOwner: {
                    width: 110,
                    head: '查询实收明细',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchIncomeSummary: {
                    width: 110,
                    head: '查询实收汇总',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchIncomeDetails: {
                    width: 110,
                    head: '查询实收汇总明细',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchIncomePersonal: {
                    width: 110,
                    head: '查询个人实收',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchAchievementStat: {
                    width: 110,
                    head: '查询业绩统计',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchBargainStat: {
                    width: 110,
                    head: '查询成交统计',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchAchievementDetail: {
                    width: 110,
                    head: '查询我的业绩',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchPersonalRank: {
                    width: 130,
                    head: '查询个人业绩排行榜',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchInCome: {
                    width: 110,
                    head: '查询运营收入',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchTurnover: {
                    width: 110,
                    head: '查询运营成交',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchPreliminary: {
                    width: 110,
                    head: '查询预审',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchLoan: {
                    width: 110,
                    head: '查询贷款',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchLitigation: {
                    width: 130,
                    head: '查询诉讼',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchNotLitigation: {
                    width: 110,
                    head: '查询非诉',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchTransfer: {
                    width: 110,
                    head: '查询过户明细',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchLinkage: {
                    width: 110,
                    head: '查询联动业绩',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                },
                SearchCharge: {
                    width: 110,
                    head: '查询服务费收据',
                    renderer: function(val, c, row) {
                        if (val)
                            return '√';
                        else
                            return '';
                    }
                }
            },
            scope: this
        });
    },
    initEvent: function() {
        var _this = this;
        $('#btn_add').click(function() {
            _this.add('empsetting');
            //_this.form.reset();
        });
        $('#btn_edit').click(function() {
            _this.editSetting();
        });
        $('#btn_del').click(function() {

            _this.delList();
            // var _idList=[];
            // for(var i=0;i<_this.idList.length;i++){				
            // if(!$('#'+_this.idList[i]+' td:eq(5)').text()){
            // _idList.push(_this.idList[i]);
            // }
            // }
            // _this.del('','确定删除这几条设定记录吗？\n隶属设定将不会删除！','',{idlist:_idList.join(',')},_idList.length);
        });
        $('#btn_deldepart').click(function() {
            _this.delDepart();
        });

        $('#chk_all').live('click', function() {
            _this.idList = [];
            if (this.checked) {
                $(':checkbox[name="chk_record"]').each(function() {
                    _this.idList.push(this.id.substr(4));
                    $('#' + this.id.substr(4)).addClass('selected_orgbgcolor');
                });
            } else {
                $(':checkbox[name="chk_record"]').each(function() {
                    $('#' + this.id.substr(4)).removeClass('selected_orgbgcolor');
                });
            }
            $(':checkbox[name="chk_record"]').attr('checked', this.checked);
        });

        $('#' + this.grid.renderTo + ' :checkbox[name="chk_record"]').live('click', function(event) {
            if (this.checked) {
                _this.idList.push(this.id.substr(4));
                $('#' + this.id.substr(4)).addClass('selected_orgbgcolor');
            } else {
                _this.idList.remove(this.id.substr(4));
                $('#' + this.id.substr(4)).removeClass('selected_orgbgcolor');
            }

            if (_this.idList.length == 1) {
                _this.currentId = _this.idList[0];
            } else {
                _this.currentId = undefined;
            }
        });

        /*
        $('#' + this.grid.renderTo + ' .tr_empsettinglist').live('click', function(e) {
        if (e.target.tagName == 'INPUT') {
        return;
        }
        var _chk = $('#chk_' + this.id);

			$('#' + _this.grid.renderTo + ' .selected_orgbgcolor').removeClass('selected_orgbgcolor');
        $('#' + _this.grid.renderTo + ' :checked[name="chk_record"]').attr('checked', false);
        _this.idList = [];
        _this.idList.push(this.id);
        _this.currentId = this.id;
        $(this).addClass('selected_orgbgcolor');
        _chk.attr('checked', true);

		});

		$('.tr_empsettinglist').live('mouseover', function() {
        $(this).toggleClass('orgbgcolor');
        });
        $('.tr_empsettinglist').live('mouseout', function() {
        $(this).toggleClass('orgbgcolor');
        });
        */

    },
    createForm: function() {
        return new Chenxin.UI.EmpSettingForm({
            scope: this,
            op: 'empsetting',
            saveSucc: function() {
                this.reloadGrid();
            },
            autoHide: false,
            afterGetFormJson: function(model) {
                if (model.settingtype == '10') {//门店
                    if (model.role) {
                        delete model.role;
                    }
                    if (model.usercode) {
                        delete model.usercode;
                    }
                    Chenxin.apply(this.form.additionalParam, model);
                } else if (model.settingtype == '20') {//个人
                    if (model.depid) {
                        delete model.depid;
                    }
                    // var _usercode=this.form.findByName('usercode');
                    // if(_usercode.currentNode){			
                    // model.usercode=this.form.findByName('usercode').currentNode.code;
                    // }
                    Chenxin.apply(this.form.additionalParam, model);
                } else {
                    //var _usercode=this.form.findByName('usercode');
                    // if(_usercode.currentNode){		
                    // this.form.additionalParam.usercode=_usercode.currentNode.code;
                    // this.form.additionalParam.userid=model.usercode;
                    // }
                    // var _aimusercode=this.form.findByName('aimusercode');
                    // if(_aimusercode.currentNode){			
                    // this.form.additionalParam.aimusercode=_aimusercode.currentNode.code;
                    // this.form.additionalParam.aimuserid=model.aimusercode;	
                    // }	
                    Chenxin.apply(this.form.additionalParam, model);
                    this.form.additionalParam.settingtype = 30;
                }
                Chenxin.Fn.initObject(model);
            }
        });
    },
    createSearch: function() {
        return new Chenxin.UI.EmpSettingSearch({
            scope: this
        });
    },
    editSetting: function() {
        if (!this.currentId) {
            alert('请选择一条记录进行编辑');
            return;
        }
        if (!this.editForm) {
            this.editForm = new Chenxin.UI.EmpSettingEditForm({
                scope: this,
                op: 'edit',
                saveSucc: function() {
                    this.reloadGrid();
                }
            });
        }
        if (this.editForm.show()) {
            Chenxin.Fn.getModel(this.currentId, this.actionUrl, function(model) {
                this.fitData(model);
                this.scope.currentModel = model;
            }, this.editForm);
        }
    },
    reloadGrid: function() {
        Chenxin.UI.EmpSettingMgr.superclass.reloadGrid.call(this);
        this.idList = [];
        this.currentId = undefined;
    },
    delDepart: function() {
        if (!this.delDepartForm) {
            this.delDepartForm = new Chenxin.UI.DelDepartForm({
                scope: this,
                op: 'delbydep',
                saveSucc: function() {
                    this.reloadGrid();
                }
            });
        }
        this.delDepartForm.show();
    },
    initComponent: function() {
        Chenxin.UI.EmpSettingMgr.superclass.initComponent.call(this);
    }
});

Chenxin.UI.EmpSettingSearch=Chenxin.extend(Chenxin.UI.SearchCommon,{
	id:'esm_search',
	initEvent:function(){		
		var _this=this;
		$('#btn_search').live('click',function(){
			_this.scope.reloadGrid("",0);
		});
	},
	createComboList:function(){
		
        var depart=new Chenxin.UI.DepartTreeCombo({
            textBoxId:'esm_depart',
			name:'departid'
        });
        
        
//        var user=new Chenxin.UI.UserTreeCombo({
//            textBoxId:'esm_user',
//			name:'userid'
//        });
//        return [depart, user]
        return [depart]
	},
	initComponent:function(){		
		Chenxin.UI.EmpSettingSearch.superclass.initComponent.call(this);
	}
});

/**
*@description 人员设定编辑
*/
Chenxin.UI.DelDepartForm=Chenxin.extend(Chenxin.UI.FormCommon,{
actionUrl: '/service/logic/EmpSettingMgr.ashx',
	id:'delDepartWin',
	renderTo:'jqm_delDepartWin',
	jqmWidth:400,
	title:'删除部门设定',
	defBBtn:true,
	afterGetFormJson:function(model){
		this.delDepartForm.additionalParam.departid=model.departid
	},
	initEvent:function(){	
		var _this=this;
		var filter='#'+this.id;
		$(filter+'_Save').live('click',function(){			
			_this.save();
		});
		$(filter+'_Cancle').live('click',function(){
			_this.hide();			
		});	
	},
	createComboList:function(){
		var depart=new Chenxin.UI.DepartTreeCombo({
            textBoxId:this.id+'_dep',
			name:'departid',
            singleSelect:true
        });
		return [depart];
	},
	createForm:function(){
		return Chenxin.FormCommon.delDepartForm(this.id);
	},
	initComponent:function(){
		Chenxin.UI.DelDepartForm.superclass.initComponent.call(this);
	}
	
});

/**
*@description 表单内容
*/
Chenxin.FormCommon.delDepartForm=function(comboIdPrefix){
return '<table class="tbl_f"><tr><th style="width:120px;">请选择部门：</th><td><input id="' + comboIdPrefix + '_dep" class="inputbox_n fl" type="text"  style="width:110px;margin-left: 5px;" /></td></tr><tr><td></td><td class="isfilled">将删除该部门所有非隶属的业务设定。</td></tr><tr><td colspan="2"><div class="tdline"></div></td></tr></table>';
}


/**
*@description 人员设定编辑
*/
Chenxin.UI.EmpSettingEditForm=Chenxin.extend(Chenxin.UI.FormCommon,{
	actionUrl:'/service/logic/EmpSettingMgr.ashx',
	id:'settingEditWin',
	renderTo:'jqm_settingEditWin',
	jqmWidth:700,
	title:'人员设定编辑',
	defBBtn:true,
	afterGetFormJson:function(model){
		var _p=',';
		for(var i=0;i<comboCommon.purview.length;i++){
			_p+=comboCommon.purview[i].val+',';
		}
		var _data={};
		for(var p in this.currentModel){
			if(_p.indexOf(','+p+',')==-1){
				_data[p]=this.currentModel[p];
			}
		}
		Chenxin.applyIf(model,_data);
	},
	initEvent:function(){	
		var _this=this;
		var filter='#'+this.id;
		$(filter+'_Save').live('click',function(){			
			_this.save();
		});
		$(filter+'_Cancle').live('click',function(){
			_this.hide();			
		});	
		this.initPreview();
	},
	initPreview:function(){
		var html='';
		var items=comboCommon.purview;
		for(var i=0;i<items.length;i++){
			var t=Math.random()*100000+'';
			var id='cx_'+t.split('.')[0]+'_'+t.split('.')[1];
			html+='<div style="float:left; width:130px;"><input type="checkbox" name="'+items[i].val+'" value="true" id="'+id+'"/><label for="'+id+'">'+items[i].txt+'</label></div>';
		}
		$('#td_'+this.id+'_purview').html(html);
	},
	createForm:function(){
		return Chenxin.FormCommon.empSettingEditForm(this.id);
	},
	initComponent:function(){
		Chenxin.UI.EmpSettingEditForm.superclass.initComponent.call(this);
	}
	
});

/**
*@description 表单内容
*/
Chenxin.FormCommon.empSettingEditForm=function(comboIdPrefix){
return '<table class="tbl_f"><tr><th style="width:100px;">工号：</th><td><input name="UserCode" class="inputbox_n fl" type="text"  style="width:160px;margin-left: 5px;" /></td><th>姓名：</th><td><input name="UserName" class="inputbox_n fl" type="text"  style="width:160px;margin-left: 5px;" /></td></tr><tr><th style="width:100px;">公司：</th><td><input name="Company" class="inputbox_n fl" type="text"  style="width:160px;margin-left: 5px;" /></td><th>部门：</th><td><input name="DepartName" class="inputbox_n fl" type="text"  style="width:160px;margin-left: 5px;" /></td></tr><tr><th>权限设定：</th><td colspan="3" id="td_' + comboIdPrefix + '_purview"></td></tr></table>';
}

getcity = true; //设置需要获取城市下拉框数据

$(document).ready(function(){
	var esm=new Chenxin.UI.EmpSettingMgr();
});

